<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>我的申请</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/layuiadmin/style/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body" style="padding: 15px;">
            <form class="layui-form" action="" id="app-apply" lay-filter="component-form-group">
                <input type="hidden" id="batchId" name="batchId">
                <div class="layui-form-item">
                    <label class="layui-form-label">当前批次</label>
                    <div class="layui-input-block">
                        <input type="text" id="curBatch" readonly class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item layui-form-text">
                    <label class="layui-form-label">申请理由</label>
                    <div class="layui-input-block">
                        <textarea name="applicationReason" class="layui-textarea"></textarea>
                    </div>
                </div>

                <div class="layui-form-item layui-layout-admin">
                    <div class="layui-input-block">
                        <div>
                            <button class="layui-btn" lay-submit="" lay-filter="application-save">提交申请</button>
                        </div>
                    </div>
                </div>
            </form>
            <table class="layui-table" id="app-detail">
                <tr>
                    <td>当前批次</td>
                    <td id="batch"></td>
                </tr>
                <tr>
                    <td>姓名</td>
                    <td id="name"></td>
                </tr>
                <tr>
                    <td>学号</td>
                    <td id="sn"></td>
                </tr>
                <tr>
                    <td>所选寒衣</td>
                    <td id="pro">

                    </td>
                </tr>
            </table>
        </div>
    </div>
</div>


<script src="/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'form', 'laydate', 'upload','layedit'], function () {
        var $ = layui.$
            , admin = layui.admin,
            layedit = layui.layedit
            , element = layui.element
            , layer = layui.layer
            , laydate = layui.laydate,
            upload = layui.upload
            , form = layui.form;
        //判断当前批次是否已有申请
        $.ajax({
            type: 'get',
            url: '/application/getBySnAndBatchId',
            success: function (res) {
                if(res.data != null){
                    $("#app-apply").hide()
                    //展示详情之前，先渲染详情
                    $("#batch").text(res.data.batchName)
                    $("#name").text(res.data.name)
                    $("#sn").text(res.data.sn)
                    if(res.data.productId == null){
                        $("#pro").html("还没有选择寒衣<button class='layui-btn layui-btn-success'>点击选择</button>")
                        //这里没有写完
                    }
                    $("#app-detail").show()
                }else{
                    $("#app-apply").show()
                    $("#app-detail").hide()
                }
            }
        })
        $.ajax({
            type: 'get',
            url: '/batchSetting/curBatch',
            success: function (res) {
                if (res.data == null) {
                    layer.msg("不存在任何批次，请先设置批次")
                    return false
                }
                $("#curBatch").val(res.data.name)
                $("#batchId").val(res.data.id)
                form.render()
            }
        })
        form.on("submit(application-save)",function (obj) {

            $.ajax({
                type: 'post',
                url: '/application/apply',
                data: obj.field,
                success: function (res) {
                    if(res.data){
                        layer.msg("申请成功",{icon: 6},function () {
                            //跳转到学生申请历史
                        })
                    }else {
                        layer.msg("当前批次已提交申请，无法再次提交",{icon: 6})
                    }
                }
            })
            return false
        })
    });
</script>
</body>
</html>
